//
//  SYBaseViewController.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/7/4.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "SYBaseViewController.h"
#import "FRNavigationItemButton.h"



@interface SYBaseViewController ()



@end

@implementation SYBaseViewController

- (SYNavigationController *)syNavController
{
    return (SYNavigationController *)self.navigationController;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navBar = [[SYNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SafeAreaTopHeight)];
        [self setUp];
        
    }
    return self;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self setUp];
    
}


- (id)init
{
    self = [super init];
    if (self) {
        self.navBar = [[SYNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SafeAreaTopHeight)];
        [self setUp];
    }
    return self;
}

- (void)setUp
{

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;

    self.hidesBottomBarWhenPushed = self.isRootViewController?NO:YES;
    
}

- (void)loadView
{
    [super loadView];
    
    self.view.backgroundColor = colorFromHex(@"f4f5f7");
    self.view.autoresizesSubviews = YES;

    _navBar.hidden = NO;
    [self.view addSubview:_navBar];
    
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if (_isRootViewController) {
        
        self.navigationItem.leftBarButtonItem = nil;
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //_navBar.backgroundColor = colorFromHex(@"#3bc144");
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    }
    
    if (!_isRootViewController) {
        [self addLeftBarButtonItemWithTitle:nil target:self action:@selector(back) backgroundImage:[UIImage imageNamed:@"all_back_"]];
    }
    
    self.networkErrorView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, /*(SCREENHEIGH-64-175)/2+*/SafeAreaTopHeight, SCREENWIDTH, /*175*/SCREENHEIGH - SafeAreaTopHeight) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    self.networkErrorView.userInteractionEnabled= YES;
    self.networkErrorView.delegate = self;
    [self.view addSubview:self.networkErrorView];
    
}

- (void)viewDidAppear:(BOOL)animated {
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognize)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    // Add code to clean up any of your own resources that are no longer necessary.
    
    // 此处做兼容处理需要加上ios6.0的宏开关，保证是在6.0下使用的,6.0以前屏蔽以下代码，否则会在下面使用self.view时自动加载viewDidLoad
    if ([self.view window] == nil)// 是否是正在使用的视图
    {
        // Add code to preserve data stored in the views that might be
        // needed later.
        
        // Add code to clean up other strong references to the view in
        // the view hierarchy.
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
    }
    // Dispose of any resources that can be recreated.
}

- (void)addLeftBarButtonItemWithTitle:(NSString *)title target:(id)target action:(SEL)action backgroundImage:(UIImage *)bgImage
{
    
    CGFloat width = bgImage ? (bgImage.size.width / bgImage.size.height * 25) : 30;
    FRNavigationItemButton *leftButton = [FRNavigationItemButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, width, 25)];
    [leftButton setTitle:title forState:UIControlStateNormal];
    [leftButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [leftButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [leftButton setImage:bgImage forState:UIControlStateNormal];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    if (!bgImage) {
        [leftButton sizeToFit];
    }
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navBar.navigationItem.leftBarButtonItem = leftBarButton;
}



#pragma mark - Actions
- (void)backButtonTapped:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
